//
//  common.h
//  myvpn
//
//  Created by Zhuobin He on 12-12-31.
//  Copyright (c) 2012年 Zhuobin He. All rights reserved.
//


#ifndef myvpn_common_h
#define myvpn_common_h

#include "SysLib.h"


#define max(a, b) ((a)>(b)?(a):(b))
#define min(a, b) ((a)<(b)?(a):(b))
#define WHITESPACE " \t\r\n"


void zmalloc(void *dst, size_t size);


char * trim(char *str);

bool check_str(const char *str);

int execute_cmd(const char *cmd, char *result, int limit);

struct _ClientInfo {
    int conn_id;
    int src_ip;
    int src_port;
    int sock_src;
    int sock_write;
    int sock_write2;
    int last_send_time;
    int last_recv_time;
    int last_packet_id;
    int lost_packets;
    int recv_packets;
    int next_packet_id;
    int remote_ip;
    int remote_port;
    int remote_port2;
    int renew_ver;
};
typedef struct _ClientInfo ClientInfo;
typedef ClientInfo * PClientInfo;

PClientInfo new_client_info(struct sockaddr_in *src_addr, int sock_src, struct sockaddr_in *remote_addr);

struct _Config {
    int mode;

    const char *bind_ip;
    int bind_port;

    const char *server_ip;

    const char *log_file;
    int log_level;

    int timeout;
    int report_interval;
    int renew_interval;

    int min_packet;

    const char *sync_url;
    const char *user;
};

typedef struct _Config Config;
typedef Config * PConfig;

extern int _log_level;

bool is_log_debug();

void log_error(const char *msg, ...);
void log_info(const char *msg, ...);
#ifdef LOG_DEBUG_ENABLE
void log_debug(const char *msg, ...);
void log_trace(const char *msg, ...);
#else
#define log_trace(msg, ...)
#define log_debug(msg, ...)
#endif


char * string_alloc(const char *str);
int str2int(const char *str);

struct sockaddr_in * sockaddr_by_ip_port(const char *host, int port);

PConfig load_config();

#endif
